package Demo;

import java.util.*;

public class Test {
    public static void main(String[] args) {

    }
    public static void main5(String[] args) {//ArrayList的遍历
        ArrayList<Integer> list3 = new ArrayList<>();
        list3.add(1);
        list3.add(2);
        list3.add(3);
        list3.add(4);
        list3.add(5);
        //1
        for (int i = 0; i < list3.size(); i++) {
            System.out.print(list3.get(i) +" ");
        }
        System.out.println();
        //2
        for (Integer x:list3){
            System.out.print(x + " ");
        }
        System.out.println();
        //3迭代器
        Iterator<Integer> iterator = list3.iterator();
        while (iterator.hasNext()){
            System.out.print(iterator.next() + " ");
        }
        System.out.println();
        //4
        ListIterator listIterator = list3.listIterator();
        while (listIterator.hasNext()){
            System.out.print(listIterator.next() + " ");
        }
        System.out.println();
        //5 从后往前遍历
        ListIterator listIterator2 = list3.listIterator(list3.size());
        while (listIterator2.hasPrevious()){
            System.out.print(listIterator2.previous() + " ");
        }
        System.out.println();
    }
    public static void main4(String[] args) {
        ArrayList<Integer> list3 = new ArrayList<>();
        list3.add(1);
        list3.add(2);
        list3.add(3);
        List<Integer> list = list3.subList(0,2);//[0,1)
        //在原List上截取，也就是说指向的是原List的引用，因此修改List的值同时也会修改List3的值
        list.set(0,188);
        System.out.println(list);//一般能够直接sout输出内容时，说明里面一定重写了toString方法
        System.out.println(list3);
    }
    public static void main3(String[] args) {
        ArrayList<Integer> list3 = new ArrayList<>();
        list3.add(1);
        list3.add(2);
        list3.add(3);
        list3.remove(2);//删除的是下标
        list3.remove(new Integer(2));//删除的是元素
        System.out.println(list3.get(0));//获取0下标的值
        list3.set(0,99);//修改0下标的值为99
        System.out.println(list3);
        list3.clear();//清空顺序表
    }
    public static void main2(String[] args) {
        ArrayList<Integer> list = new ArrayList<>(15);
        List<Integer> list2 = new LinkedList<>();
        list2.add(1);
        list2.add(2);
        list2.add(3);
        list2.add(4);
       ArrayList<Integer> list3 = new ArrayList<>();
        list3.add(10);//在第一次add分配大小为10的内存空间，扩容按照1.5倍的方式进行扩容,扩容的最大容量是int的最大值
        list3.add(1,100);
        list3.addAll(list2);
        System.out.println(list3);

        //常用这种
        //向上转型
        List<Integer> list1 = new ArrayList<>(list2);//会先构造list2里的内容
        list1.add(5);//默认放到数组的最后
        System.out.println(list1);
    }
    public static void main1(String[] args) {
        MyArrayList myArrayList = new MyArrayList();
        myArrayList.add(1);
        myArrayList.add(2);
        myArrayList.add(3);
        myArrayList.add(4);
        myArrayList.add(5);
        myArrayList.add(0,199);
        System.out.println(myArrayList.contains(199));
        System.out.println(myArrayList.indexOf(199));
        System.out.println(myArrayList.get(2));
        System.out.println(myArrayList.get(3));
        System.out.println(myArrayList.get(5));
        myArrayList.display();
        myArrayList.remove(0);
        myArrayList.remove(1);
        myArrayList.remove(2);
        myArrayList.set(0,9999);
        myArrayList.display();
    }
}
